package com.example.shw

import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.LinearLayout

class PinnedShortcutActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val linearLayout = LinearLayout(this)
        var button = Button(this)
        button.text = "Button"
        linearLayout.addView(button)
        setContentView(linearLayout)
        val shortcutManager = getSystemService(ShortcutManager::class.java)
        button.setOnClickListener {
            if (shortcutManager.isRequestPinShortcutSupported){
                val myIntent = Intent(this@PinnedShortcutActivity, PinnedShortcutActivity::class.java)
                myIntent.action = "android.intent.action.VIEW"
                val pinShortcutInfo = ShortcutInfo.Builder(this@PinnedShortcutActivity, "my-shortcut")
                    .setShortLabel("Pinned快捷")
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                    .setIntent(myIntent).build()
                shortcutManager.requestPinShortcut(pinShortcutInfo, null)
            }
        }
    }
}